<?php
/**
 * 《奖励情况》实现类
 *
 * @author 孙晓晔
 * @version $Id: Family.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/EmpUtil.class.php');

class Award extends EmpUtil {
	public $tbl = Constant::tbl_emp_award;
	
	public $type = array(
		'-' => '请选择',
		'1' => '嘉奖',
		'2' => '记小功',
		'3' => '记大功',
		'4' => '晋升',
	
		'9' => '其它',
	);
	
	public $result = array(
		'-' => '请选择',
		'1' => '嘉奖',
		'2' => '记小功',
		'3' => '记大功',
		'4' => '晋升',
	
		'9' => '其它',
	);
	
	public $method = array(
		'-' => '请选择',
		'1' => '罚款',
		'2' => '赔偿',
		'3' => '扣工资',

		'9' => '其它',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '正在处理',
		'3' => '处理完成',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'emp' => '员工',
		'dept' => '部门',
			
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('type_option', $this->type);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/award/find');
		return true;
	}

	function orderby($px) {
		$ret = " order by id desc";
		if (empty($px) || $px == "-") {
			return $ret;
		} else {
			return ' order by '.$px.' desc';
		}

		return $ret;
	}
	
	function _where($request) {		
		
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/award/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/award/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $request['emp'];
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$date = $request['date'];
		$address = $request['address'];
		
		$type = $request['type'];
		$name = $request['name'];
		
		$opinion1 = $request['opinion1'];
		$opinion2 = $request['opinion2'];
		
		$result = $request['result'];
		$method = $request['method'];
		$amount = $request['amount'];
		
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, date, address, type, name, opinion1, opinion2, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $address, $type, $name, $opinion1, $opinion2, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);

		$id = $db->insert_id();

		$request['id_s'] = $id;

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('emp', $this->_emp_x($id));

		$controller->display($request, 'emp/award/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $request['emp'];
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$date = $request['date'];
		$address = $request['address'];
		
		$type = $request['type'];
		$name = $request['name'];
		
		$opinion1 = $request['opinion1'];
		$opinion2 = $request['opinion2'];
		
		$result = $request['result'];
		$method = $request['method'];
		$amount = $request['amount'];
		
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', address = '%s', type = '%s', name = '%s', opinion1 = '%s', opinion2 = '%s', ";
		$fmt .= "content = '%s', modifytime = now() WHERE id = '%s' ";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $address, $type, $name, $opinion1, $opinion2, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'emp/award/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->type = $this->_shift($this->type);
		
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/award/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['type_t'] = $this->type[$row['type']];

		$row = $this->_view_t($row);
		
		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "DELETE FROM " . $this->tbl . " WHERE id " . $insql;
		// debug($sql);
		$query = $db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'emp/award/_list');
		return true;
	}
}
?>